//
//  SpeakDetailViewController.m
//  HeartOfMeet
//
//  Created by qianfeng on 16/4/8.
//  Copyright © 2016年 千云锋. All rights reserved.
//

#import "SpeakDetailViewController.h"
#import "SpeakDetailModel.h"
#import "SpeakDetailTableViewCell.h"

@interface SpeakDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UIBarButtonItem *rightBarButtonItem;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic)int offSet;
@property(nonatomic)BOOL isRefreshing;
@property(nonatomic)BOOL isLoadingMore;
@property (nonatomic,strong) NSMutableArray *speakArray;
@end
@implementation SpeakDetailViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    _speakArray = [[NSMutableArray alloc] init];
    [self navigationItemSetting];
    [self initParamater];
    [self.view addSubview:self.tableView];
    [self createRefreshHeadView];
    [self createRefreshFootView];
    [self fatchData];
}
- (void)initParamater{
    self.offSet = 0;
}
- (void)createRefreshHeadView{
    
    __weak typeof(self) wSelf = self;
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (wSelf.isRefreshing) {
            return ;
        }
        
        wSelf.isRefreshing  = YES;
        [wSelf initParamater];
        [wSelf fatchData];
    }];
}

- (void)createRefreshFootView{
    
    __weak typeof(self) wSelf = self;
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (wSelf.isLoadingMore) {
            return ;
        }
        
        wSelf.isLoadingMore = YES;
        [wSelf fatchData];
    }];
}

- (void)fatchData{
    if (![self fatchFormLocal]) {
        [self fatchFormNetwork];
    }
}

- (BOOL)fatchFormLocal{
    return NO;
}

- (void)fatchFormNetwork{
    //掉落动画
    [MMProgressHUD setPresentationStyle:MMProgressHUDPresentationStyleDrop];
    [MMProgressHUD showWithTitle:@"努力加载中..."];
    
    NSString * str = [NSString stringWithFormat:SPEAKDETAILURL,_offSet,_userId];
    NSLog(@"%@",str);
    [[NetDataEngine sharedInstance] requestDiscoverUrl:str success:^(id respondObject) {
        if (respondObject) {
            
            NSArray *dataArray = respondObject[@"data"];
            //            NSLog(@"%@",dataArray);
            for (NSDictionary *diantaiDict in dataArray) {
                SpeakDetailModel *diantaiModel = [[SpeakDetailModel alloc] initWithDictionary:diantaiDict error:nil];
                
                [_speakArray addObject:diantaiModel];
            }
            [MMProgressHUD dismissWithSuccess:@"下载完成" title:@"ok"];
            
            [self endRefreshing];
            [_tableView reloadData];
        }
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
        [self endRefreshing];
    }];
    
}

- (void)endRefreshing{
    if (self.isRefreshing) {
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
        self.isRefreshing = NO;
    }
    if (self.isLoadingMore) {
        self.isLoadingMore = NO;
        [self.tableView footerEndRefreshing];
    }
}

- (UITableView*)tableView{
    if (_tableView ==  nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
        _tableView.backgroundColor = [UIColor blackColor];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        _tableView.separatorColor = [UIColor whiteColor];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth*200/600)];
        imageView.image = [UIImage imageNamed:@"back_image"];
        
        UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, kScreenWidth*200/600 - 60, 60, 60)];
        headerImageView.center = imageView.center;

        [headerImageView sd_setImageWithURL:[NSURL URLWithString:self.avatar] placeholderImage:[UIImage imageNamed: @"placehold_rec"]];
        headerImageView.layer.masksToBounds = YES;
        headerImageView.layer.cornerRadius = 30;
        [imageView addSubview:headerImageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, kScreenWidth*200/600 - 20, kScreenWidth, 20)];
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.text = [NSString stringWithFormat:@"收听 %@ | 关注 %@",self.viewnum,self.favnum];
        [imageView addSubview:label];
        
        _tableView.tableHeaderView = imageView;
    }
    UINib *nib = [UINib nibWithNibName:@"SpeakDetailTableViewCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"cellID"];
    return _tableView;
}


- (void)navigationItemSetting{
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[[UIImage imageNamed: @"white_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    UIBarButtonItem *rightItem1 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed: @"yinle_"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(push)];
    
    self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed: @"like_0"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(likeClick:)];
    
    self.navigationItem.rightBarButtonItems = @[rightItem1,self.rightBarButtonItem];
}
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)push{
    [UIView  beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelay:0.1];
    [self.navigationController pushViewController:[PlayerViewController sharePlayer] animated:NO];
    [UIView commitAnimations];
}

- (void)likeClick:(UIBarButtonItem *)item{

}

#pragma mark -
#pragma mark delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _speakArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"cellID";
    SpeakDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    SpeakDetailModel *model = _speakArray[indexPath.row];
    cell.titleLabel.text = model.title;
    cell.numberLabel.text = [NSString stringWithFormat:@"收听：%@",model.viewnum];
    [cell.myImageVIew sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed: @"touxiang01.png"]];
    cell.myImageVIew.layer.masksToBounds = YES;
    cell.myImageVIew.layer.cornerRadius = 10;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SpeakDetailModel *model = self.speakArray[indexPath.row];
    
    SpeakDetailTableViewCell *cell = (SpeakDetailTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    CGFloat y = cell.frame.origin.y - tableView.contentOffset.y;
    [LZXHelper animationPlayWithUrl:model.cover viewController:self leaderFrame:CGRectMake(8, y+64+7, 50, 50) newFrame:CGRectMake(self.view.bounds.size.width-30, 30, 6, 6)];
    
    [PlayerViewController sharePlayer].mainImageUrl = model.cover;
    [PlayerViewController sharePlayer].topicTitle = model.title;
    [PlayerViewController sharePlayer].viewNum = model.viewnum;
    [PlayerViewController sharePlayer].avaImageUrl = self.avatar;
    [PlayerViewController sharePlayer].speakNickName = self.nickname;
    
    [[PlayerViewController sharePlayer] playWithUrl:model.url];

}


@end
